package Manager;

import CODEC.Encoding;
import CODEC.Impl.EncodingImpl;
import Utils.Util;
import domain.Message;
import domain.PDU.PDU;
import domain.PDU.VariableBindings;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
 * @Author: ylc
 * @Description:发送数据
 */
public class UDPClient {
    public static void main(String[] args) throws IOException {
        Encoding encoder = new EncodingImpl();

        SocketAddress ADDRESS = new InetSocketAddress("localhost", 9999);
        DatagramSocket socket = new DatagramSocket();

        // 构建SNMP数据包
        VariableBindings var1 = new VariableBindings("2.1.11.8.0", "INTEGER", "10");
        PDU pdu = new PDU("get-request","15","noError","noError", var1);
        Message snmp = new Message("version-1","ylc",pdu);
        byte[] bytes = encoder.getSnmpMessageCoding(snmp);
        
        // 显示构建的数据包
        System.out.println("SNMP数据包长度：" + encoder.getSnmpMessageCoding(snmp).length);
        Util.showPacket(encoder.getSnmpMessageCoding(snmp));

        // 发送数据包
        DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,ADDRESS);
        socket.send(packet);
    }
}
